package ui

import applicationModel.BuscadorZombie
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.form.TextField
import org.apache.wicket.model.CompoundPropertyModel
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XButton
import org.uqbar.wicket.xtend.XListView
import org.apache.wicket.markup.html.WebPage

class AlmanaqueZombiePage extends WebPage {
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	var BuscadorZombie buscador;
	var JardinTableroPage mainPage;

	new(JardinTableroPage mainPageX) {
		mainPage = mainPageX;
		buscador = new BuscadorZombie();
		
		val Form<BuscadorZombie> buscarForm = new Form<BuscadorZombie>("buscarZombiesForm", new CompoundPropertyModel<BuscadorZombie>(this.buscador));
		this.agregarCamposBusqueda(buscarForm);
		this.agregarAcciones(buscarForm);
		this.agregarGrillaResultados(buscarForm);
		this.addChild(buscarForm);
		this.botones()
		// Al abrir el formulario disparo la búsqueda
		this.buscarZombies();
		
	}

	def buscarZombies() {
		this.buscador.search();
	}

	def agregarCamposBusqueda(Form<BuscadorZombie> buscarform) {
		buscarform.addChild(new TextField<String>("nombre"));
	}

	def agregarAcciones(Form<BuscadorZombie> buscarform) {
		val buscarButton = new XButton("buscar")
		buscarButton.onClick = [ | buscador.search ]
		buscarform.addChild(buscarButton);		
	}	

	def agregarGrillaResultados(Form<BuscadorZombie> buscarform) {
		val listView = new XListView("resultados")
		listView.populateItem = [ item |
			item.model = item.modelObject.asCompoundModel
			item.addChild(new Label("nombre"))
			item.addChild(new Label("vida"))
			item.addChild(new Label("potencia"))
			item.addChild(new Label("descripcion"));
			
		]
		buscarform.addChild(listView);
	}
	
	def botones(){
		val Form<String> form = new Form<String>("form");
		form.addChild(new XButton("volver").onClick = [|this.volverATablero()]);
		
		this.addChild(form)
	}
	
	def volverATablero() {
		responsePage = mainPage
	}
	}

